<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/blog/","images":"/blog/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/blog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="1 前言在分库分表的过程中，因为拆分的实体表的ID有可能是重复的，正式由于这个问题才会有分布式全局ID这个方案的出现。">
<meta property="og:type" content="article">
<meta property="og:title" content="分布式全局ID">
<meta property="og:url" content="http://littlefxc.github.io/2021/07/18/%E5%88%86%E5%B8%83%E5%BC%8F%E5%85%A8%E5%B1%80ID/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="1 前言在分库分表的过程中，因为拆分的实体表的ID有可能是重复的，正式由于这个问题才会有分布式全局ID这个方案的出现。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/bH5oQj-20210718211022587.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/ZvQWuo.png">
<meta property="article:published_time" content="2021-07-18T09:22:17.000Z">
<meta property="article:modified_time" content="2021-07-19T15:00:20.860Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="分布式">
<meta property="article:tag" content="分布式全局ID">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/littlefxc/oss/raw/master/images/bH5oQj-20210718211022587.png">


<link rel="canonical" href="http://littlefxc.github.io/2021/07/18/%E5%88%86%E5%B8%83%E5%BC%8F%E5%85%A8%E5%B1%80ID/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>分布式全局ID | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/blog/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/blog/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/blog/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/blog/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/blog/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E5%89%8D%E8%A8%80"><span class="nav-text">1 前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-%E6%A6%82%E8%A6%81"><span class="nav-text">2 概要</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8%E7%9A%84%E7%B3%BB%E7%BB%9F%E4%B8%AD%EF%BC%8C%E7%94%B1%E4%BA%8Eid%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-text">3 分库分表的系统中，由于id引发的问题</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-%E5%88%86%E5%B8%83%E5%BC%8FID%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="nav-text">4 分布式ID的解决方案</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#4-1-UUID"><span class="nav-text">4.1 UUID</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-2-%E7%BB%9F%E4%B8%80ID%E5%BA%8F%E5%88%97"><span class="nav-text">4.2 统一ID序列</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-3-%E9%9B%AA%E8%8A%B1%E7%AE%97%E6%B3%95"><span class="nav-text">4.3 雪花算法</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">184</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">115</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/18/%E5%88%86%E5%B8%83%E5%BC%8F%E5%85%A8%E5%B1%80ID/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          分布式全局ID
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-18 17:22:17" itemprop="dateCreated datePublished" datetime="2021-07-18T17:22:17+08:00">2021-07-18</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-07-19 23:00:20" itemprop="dateModified" datetime="2021-07-19T23:00:20+08:00">2021-07-19</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="1-前言"><a href="#1-前言" class="headerlink" title="1 前言"></a>1 前言</h1><p>在分库分表的过程中，因为拆分的实体表的ID有可能是重复的，正式由于这个问题才会有分布式全局ID这个方案的出现。</p>
<span id="more"></span>

<h1 id="2-概要"><a href="#2-概要" class="headerlink" title="2 概要"></a>2 概要</h1><ul>
<li><p>分库分表的系统中，由于id引发的问题</p>
<p>它是由于什么原因导致的？它会对业务系统引发什么问题？</p>
</li>
<li><p>分布式ID的解决方案</p>
<ol>
<li>使用UUID作为id实现主键全局ID唯一性保证</li>
<li>通过统一ID序列表，实现全局ID</li>
<li>雪花算法作为全局ID</li>
<li>多种方案的比较</li>
</ol>
</li>
</ul>
<h1 id="3-分库分表的系统中，由于id引发的问题"><a href="#3-分库分表的系统中，由于id引发的问题" class="headerlink" title="3 分库分表的系统中，由于id引发的问题"></a>3 分库分表的系统中，由于id引发的问题</h1><p>它是由于什么原因导致的？它会对业务系统引发什么问题？</p>
<ul>
<li>每个表通常都会有唯一标识，通常使用id</li>
<li>ID通常采用自增的方式</li>
<li>在分库分表的情况下，每张表的id都是从0开始自增的</li>
<li>两个分片表中存在相同的 id</li>
<li>从而导致业务混乱</li>
</ul>
<h1 id="4-分布式ID的解决方案"><a href="#4-分布式ID的解决方案" class="headerlink" title="4 分布式ID的解决方案"></a>4 分布式ID的解决方案</h1><h2 id="4-1-UUID"><a href="#4-1-UUID" class="headerlink" title="4.1 UUID"></a>4.1 UUID</h2><ul>
<li><p>UUID 通用唯一识别码（Universally Unique Identifier）。</p>
</li>
<li><p>使用 UUID，保证每一条的记录id都是不同的。</p>
</li>
<li><p>缺点1：只是单纯的一个id，没有实际意义。长度32位，太长。</p>
</li>
<li><p>mycat不支持UUID的方式。</p>
</li>
<li><p>sharding-Jdbc支持UUID的方式。</p>
</li>
</ul>
<h2 id="4-2-统一ID序列"><a href="#4-2-统一ID序列" class="headerlink" title="4.2 统一ID序列"></a>4.2 统一ID序列</h2><ul>
<li><p>ID 的值统一的从一个集中的ID序列生成器中获取</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/bH5oQj-20210718211022587.png"></p>
</li>
<li><p>ID序列生成器MyCat支持，Sharding-Jdbc不支持</p>
</li>
<li><p>MyCat中有两种方式：本地文件方式、数据库方式</p>
</li>
<li><p>本地文件方式用于测试、数据库方式用于生产</p>
</li>
<li><p>优点：ID集中管理，避免重复</p>
</li>
<li><p>缺点：并发量大时，ID生成器压力较大</p>
</li>
</ul>
<h2 id="4-3-雪花算法"><a href="#4-3-雪花算法" class="headerlink" title="4.3 雪花算法"></a>4.3 雪花算法</h2><ul>
<li>SnowFlake是由Twitter提出的分布式ID算法</li>
<li>一个64 bit 的 long 型的数字（是二进制数，在程序中会转化为10进制数）</li>
<li>引入了时间戳，保持自增</li>
</ul>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/ZvQWuo.png"></p>
<ul>
<li>基本保持全局唯一，毫秒内并发最大 4096 个ID</li>
<li>时间回调，可能引起ID重复</li>
<li>MyCat 和 Sharding-Jdbc 均支持雪花算法</li>
<li>Sharding-Jdbc 可设置最大容忍回调时间</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/%E5%88%86%E5%B8%83%E5%BC%8F/" rel="tag"># 分布式</a>
              <a href="/blog/tags/%E5%88%86%E5%B8%83%E5%BC%8F%E5%85%A8%E5%B1%80ID/" rel="tag"># 分布式全局ID</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/blog/2021/07/11/%E5%9F%BA%E4%BA%8ESharding-Jdbc%E7%9A%84%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E5%92%8C%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/" rel="prev" title="基于Sharding-Jdbc的读写分离和分库分表">
                  <i class="fa fa-chevron-left"></i> 基于Sharding-Jdbc的读写分离和分库分表
                </a>
            </div>
            <div class="post-nav-item">
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/blog/js/utils.js"></script><script src="/blog/js/motion.js"></script><script src="/blog/js/schemes/muse.js"></script><script src="/blog/js/next-boot.js"></script>

  
<script src="/blog/js/local-search.js"></script>






  





</body>
</html>
